﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TestTableDataExtractionEditor : Form, IComponentEditor
    {
        #region fields
        private object _ActivityNode;
        private TestTableDataExtractionComponent _TestTableDataExtractComponent;
        #endregion

        #region init
        /// <summary>
        /// 
        /// </summary>
        public TestTableDataExtractionEditor()
        {
            InitializeComponent();
        }

        private void TestTableDataExtractionEditor_Load(object sender, EventArgs e)
        {

        }

        #endregion


        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(TestTableDataExtractionComponent); }
        }

        public void LoadSettings(
            GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._TestTableDataExtractComponent = (TestTableDataExtractionComponent) wfComponent;

            this.txt_DocList.Text = this._TestTableDataExtractComponent.OurDocListResultFilePath;
            this.txt_TableScan.Text = this._TestTableDataExtractComponent.OurTableScanResultFilePath;
            this.txt_TableLayout.Text = this._TestTableDataExtractComponent.OurTableLayoutResultFilePath;
            this.txt_TableExtract.Text = this._TestTableDataExtractComponent.OurTableExtractResultFolderPath;

            this.txt_ConnStr.Text = this._TestTableDataExtractComponent.CorrectResultConnStr;
            this.rt_Sql.Text = this._TestTableDataExtractComponent.CorrectResultSelectSql;
            if(!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultConnStr) && 
                (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultSelectSql)))
            {
                TableColumn[] cols =
                    DbUtil.GetTableColumns(
                        DataProviderType.MSSQL,
                        this._TestTableDataExtractComponent.CorrectResultConnStr,
                        this._TestTableDataExtractComponent.CorrectResultSelectSql);
                this.cbo_PageNumField.Items.Clear();
                this.cbo_DocIDField.Items.Clear();
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_PageNumField.Items.Add(col.ColumnName);
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                    }
                }
            }
            if(this.cbo_DocIDField.Items.Count>0 && (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultDocIDField)))
            {
                for(int i=0;i<this.cbo_DocIDField.Items.Count;i++)
                {
                    if(this.cbo_DocIDField.Items[i].ToString()==
                        this._TestTableDataExtractComponent.CorrectResultDocIDField)
                    {
                        this.cbo_DocIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if(this.cbo_PageNumField.Items.Count>0 && (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultPageNumberField)))
            {
                for(int i=0;i<this.cbo_PageNumField.Items.Count;i++)
                {
                    if(this.cbo_PageNumField.Items[i].ToString()== 
                        this._TestTableDataExtractComponent.CorrectResultPageNumberField)
                    {
                        this.cbo_PageNumField.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.dv_FieldMappings.Rows.Clear();
            TableColumn[] tgtCols = null;
            if (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultConnStr) && 
                !string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultSelectSql))
            {
                tgtCols =
                    DbUtil.GetTableColumns(DataProviderType.MSSQL,
                                           this._TestTableDataExtractComponent.CorrectResultConnStr,
                                           this._TestTableDataExtractComponent.CorrectResultSelectSql);
            }
            if (this._TestTableDataExtractComponent.SrcTgtFieldMappings != null &&
                this._TestTableDataExtractComponent.SrcTgtFieldMappings.Count > 0)
            {
                foreach (string srcFieldName in this._TestTableDataExtractComponent.SrcTgtFieldMappings.Keys)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellSrc = new DataGridViewTextBoxCell();
                    cellSrc.Value = srcFieldName;
                    dr.Cells.Add(cellSrc);
                    DataGridViewComboBoxCell cellTgt = new DataGridViewComboBoxCell();
                    if (tgtCols != null && tgtCols.Length > 0)
                    {
                        foreach (TableColumn col in tgtCols)
                        {
                            cellTgt.Items.Add(col.ColumnName);
                        }
                    }
                    cellTgt.Value = this._TestTableDataExtractComponent.SrcTgtFieldMappings[srcFieldName];
                    dr.Cells.Add(cellTgt);
                    this.dv_FieldMappings.Rows.Add(dr);
                }
            }
            else
            {
                foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                {
                    if (dr.IsNewRow)
                    {
                        DataGridViewComboBoxCell cellTgt = (DataGridViewComboBoxCell)dr.Cells[1];
                        if (tgtCols != null && tgtCols.Length > 0)
                        {
                            foreach (TableColumn col in tgtCols)
                            {
                                cellTgt.Items.Add(col.ColumnName);
                            }
                        }
                    }
                }
            }

            // result 
            this.txt_DocListPerformanceFilePath.Text = this._TestTableDataExtractComponent.DocListPerformanceResultFilePath;
            this.txt_TableScanPerformanceFilePath.Text = this._TestTableDataExtractComponent.TableScanPerformanceResultFilePath;
            this.txt_TableLayoutPerformanceFilePath.Text = this._TestTableDataExtractComponent.TableScanPerformanceResultFilePath;
            this.txt_TableExtractPerformanceFilePath.Text = this._TestTableDataExtractComponent.TableExtractPerformanceResultFilePath;
        }

        public void SaveSettings()
        {
            this._TestTableDataExtractComponent.OurDocListResultFilePath = this.txt_DocList.Text;
            this._TestTableDataExtractComponent.OurTableScanResultFilePath = this.txt_TableScan.Text;
            this._TestTableDataExtractComponent.OurTableLayoutResultFilePath = this.txt_TableLayout.Text;
            this._TestTableDataExtractComponent.OurTableExtractResultFolderPath = this.txt_TableExtract.Text;

            this._TestTableDataExtractComponent.CorrectResultConnStr = this.txt_ConnStr.Text;
            this._TestTableDataExtractComponent.CorrectResultSelectSql = this.rt_Sql.Text;
            this._TestTableDataExtractComponent.CorrectResultDocIDField = this.cbo_DocIDField.Text;
            this._TestTableDataExtractComponent.CorrectResultPageNumberField = this.cbo_PageNumField.Text;

            this._TestTableDataExtractComponent.SrcTgtFieldMappings = new Dictionary<string, string>();
            foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if (!dr.IsNewRow && dr.Cells[0].Value != null && dr.Cells[1].Value != null)
                {
                    string srcField = dr.Cells[0].Value.ToString();
                    string tgtField = dr.Cells[1].Value.ToString();
                    this._TestTableDataExtractComponent.SrcTgtFieldMappings.Add(srcField, tgtField);
                }
            }

            this._TestTableDataExtractComponent.DocListPerformanceResultFilePath = this.txt_DocListPerformanceFilePath.Text;
            this._TestTableDataExtractComponent.TableScanPerformanceResultFilePath = this.txt_TableScanPerformanceFilePath.Text;
            this._TestTableDataExtractComponent.TableLayoutPerformanceResultFilePath = this.txt_TableLayoutPerformanceFilePath.Text;
            this._TestTableDataExtractComponent.TableExtractPerformanceResultFilePath = this.txt_TableExtractPerformanceFilePath.Text;
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._TestTableDataExtractComponent);
            }

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dv_FieldMappings_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if (dr.IsNewRow)
                {
                    DataGridViewComboBoxCell cellTgtField = (DataGridViewComboBoxCell)dr.Cells[1];
                    if (!string.IsNullOrEmpty(this.txt_ConnStr.Text) && 
                        !string.IsNullOrEmpty(this.rt_Sql.Text))
                    {
                        TableColumn[] cols = DbUtil.GetTableColumns(
                            DataProviderType.MSSQL, this.txt_ConnStr.Text,
                            this.rt_Sql.Text);
                        if (cols != null && cols.Length > 0)
                        {
                            foreach (TableColumn col in cols)
                            {
                                cellTgtField.Items.Add(col.ColumnName);
                            }
                        }
                    }
                }
            }
        }

        private void btn_BrowseDocList_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_DocList.Text = dlg.FileName;
            }
        }

        private void btn_BrowseTableScan_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableScan.Text = dlg.FileName;
            }
        }

        private void btn_BrowseTableLayout_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableLayout.Text = dlg.FileName;
            }
        }

        private void btn_BrowseTableExtract_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableExtract.Text = dlg.SelectedPath;
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_ConnStr.Text) &&
                !string.IsNullOrEmpty(this.rt_Sql.Text))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL,
                    this.txt_ConnStr.Text, this.rt_Sql.Text);
                this.cbo_DocIDField.Items.Clear();
                this.cbo_PageNumField.Items.Clear();
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                        this.cbo_PageNumField.Items.Add(col.ColumnName);
                    }

                    foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                    {
                        if (dr.IsNewRow)
                        {
                            DataGridViewComboBoxCell cellTgtField = (DataGridViewComboBoxCell)dr.Cells[1];
                            if (!string.IsNullOrEmpty(this.txt_ConnStr.Text) &&
                                !string.IsNullOrEmpty(this.rt_Sql.Text))
                            {
                                foreach (TableColumn col in cols)
                                {
                                    cellTgtField.Items.Add(col.ColumnName);
                                }
                            }
                        }
                    }
                }
            }

            if (this.cbo_DocIDField.Items.Count > 0 && (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultDocIDField)))
            {
                for (int i = 0; i < this.cbo_DocIDField.Items.Count; i++)
                {
                    if (this.cbo_DocIDField.Items[i].ToString() ==
                        this._TestTableDataExtractComponent.CorrectResultDocIDField)
                    {
                        this.cbo_DocIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_PageNumField.Items.Count > 0 && (!string.IsNullOrEmpty(this._TestTableDataExtractComponent.CorrectResultPageNumberField)))
            {
                for (int i = 0; i < this.cbo_PageNumField.Items.Count; i++)
                {
                    if (this.cbo_PageNumField.Items[i].ToString() ==
                        this._TestTableDataExtractComponent.CorrectResultPageNumberField)
                    {
                        this.cbo_PageNumField.SelectedIndex = i;
                        break;
                    }
                }
            }

            
        }

        private void btn_Save1_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_DocListPerformanceFilePath.Text = dlg.FileName;
            }
        }

        private void btn_Save2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableScanPerformanceFilePath.Text = dlg.FileName;
            }
        }

        private void btn_Save3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableLayoutPerformanceFilePath.Text = dlg.FileName;
            }
        }

        private void btn_Save4_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TableExtractPerformanceFilePath.Text = dlg.FileName;
            }
        }
    }
}
